#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    client = new QTcpSocket(this);
    connect(client, &QTcpSocket::connected,
            this, &Widget::on_connected);
    ui->angleSlider1->setRange(0,   180);
    ui->angleSlider2->setRange(100, 180);
    ui->angleSlider3->setRange(90,  170);
    ui->angleSlider4->setRange(52,  70);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_connectButton_clicked()
{
    client->connectToHost(ui->ipEdit->text(), 23);
}

void Widget::on_connected()
{
    ui->ipEdit->setDisabled(true);
}

void Widget::on_angleSlider1_valueChanged(int value)
{
    qDebug() << value;
    QString cmd = QString("{\"servo\":0, \"angle\":%1}\r\n").arg(value);
    client->write(cmd.toUtf8());
}

void Widget::on_angleSlider2_valueChanged(int value)
{
    qDebug() << value;
    QString cmd = QString("{\"servo\":1, \"angle\":%1}\r\n").arg(value);
    client->write(cmd.toUtf8());
}

void Widget::on_angleSlider3_valueChanged(int value)
{
    qDebug() << value;
    QString cmd = QString("{\"servo\":2, \"angle\":%1}\r\n").arg(value);
    client->write(cmd.toUtf8());
}

void Widget::on_angleSlider4_valueChanged(int value)
{
    qDebug() << value;
    QString cmd = QString("{\"servo\":3, \"angle\":%1}\r\n").arg(value);
    client->write(cmd.toUtf8());
}

